<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label-width="40" label="ID" prop="id">
        <el-input type="number" v-model="queryParams.id" placeholder="ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>  
      <el-form-item label="配比名称" prop="ratioName">
        <el-input v-model.trim="queryParams.ratioName" placeholder="请输入配比名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主标号" prop="primaryDesignatorId">
        <el-input v-model.trim="queryParams.primaryDesignatorId" placeholder="请输入主标号名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" label-width="42px" prop="status">
        <el-select v-model="queryParams.status">
          <el-option :value="item.value" :label="item.label" v-for="(item, index) in ratioStatus"
            :key="index"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:designator:add']" class="button-margin">新增</el-button>
        <el-dropdown @command="(command) => handleExport(command)">
          <el-button type="warning" icon="el-icon-download" size="mini" v-hasPermi="['system:ratio:export']">
            导出<i class="el-icon-arrow-down el-icon--right"></i>
          </el-button>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item v-for="item of exportOption" :key="item.value" :command="item.label">{{ item.label }}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>        
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="ratioList" @selection-change="handleSelectionChange"
      :header-cell-style="{ backgroundColor: '#F7F8FA' }" height="380">
      <el-table-column fixed="left" type="selection" width="55" align="center" />
      <el-table-column label="配比编号" width="105" align="center" prop="ratioNo" />
      <el-table-column label="配比名称" width="150" align="center" prop="ratioName" show-overflow-tooltip />
      <el-table-column label="主标号" align="center" prop="primaryName" />
      <!-- <el-table-column label="123目录联动id" align="center" prop="categoryInfoId" />
      
      <el-table-column label="保塑时间/h" align="center" prop="plasticHoldingTime" />
      <el-table-column label="强度等级(数字)" align="center" prop="intensityRating" /> -->
      <el-table-column label="搅拌时间" align="center" prop="stirringTime" />
      <el-table-column label="单方容量" width="150" align="center" prop="unilateralCapacity" show-overflow-tooltip />
      <el-table-column label="水胶比" width="150" align="center" prop="waterBinderRatio" show-overflow-tooltip />
      <el-table-column label="更新时间" width="150" align="center" prop="updateTime" show-overflow-tooltip />
      <el-table-column label="添加人" width="150" align="center" prop="createBy" show-overflow-tooltip />
      <!-- <el-table-column label="html内容" align="center" prop="content" /> -->
      <!-- <el-table-column label="状态0开1关" align="center" prop="status" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="matchingStatusLabel(scope.row.status)" :key="scope.row.status">{{
            matchingStatusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160px" fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:designator:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-s-check" @click="handleDelete(scope.row)"
            v-hasPermi="['system:designator:remove']" v-if="scope.row.status === 1">审核</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:designator:remove']" v-else>删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import {
  listRatio,
  getRatioDetail,
  exportList,
  delDesignator,
  addDesignator,
  updateDesignator,
} from "@/api/service/qualityCenter";
import NumberFormItem from "@/components/NumberFormItem";
import { listOnInfo } from "@/api/service/info";
import { number } from "echarts";
export default {
  components: {
    NumberFormItem,
  },
  name: "qualityCenter",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品--主标号表格数据
      ratioList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        id: null,
        ratioName: null,
        primaryDesignatorId: null,
        status: null,
      },
      // 表单参数
      form: {},
      form1: {}, // 预拌混凝土
      form2: {}, // 新增配比
      // 表单校验


      activeName: "1",
      categoryList1: [],
      categoryList2: [],
      categoryList3: [],

      formCategoryList1: [],
      formCategoryList2: [],
      formCategoryList3: [],
      editDisabled: false,
      ratioStatus: [
        {
          value: 1,
          label: "待审核",
        },
        {
          value: 2,
          label: "审核驳回",
        },
        {
          value: 3,
          label: "已启用",
        },
        {
          value: 4,
          label: "已停用",
        },        
      ],
      exportOption: [ // 导出选项
        {vaule: 1,label: '按勾选导出'},
        {vaule: 2,label: '导出全部'},
      ]
    };
  },
  created() {
    this.getList();
    this.getListOnInfo();
  },
  computed: {
    // 创建一个计算属性来根据当前行的状态返回匹配的标签
    matchingStatusLabel() {
      return (status) => {
        const matchingStatus = this.ratioStatus.find(item => item.value === status);
        // 返回找到的标签，如果没有找到则返回空字符串
        return matchingStatus ? matchingStatus.label : '';
      };
    },
  },
  methods: {
    // 开关
    changeStatus(item) {
      const form = { ...item };

      // 改变状态
      form.status = form.status == "0" ? 1 : 0;

      updateDesignator(form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        item.status = form.status;
        //this.getList();
      });
    },
    formChangeOne() {
      this.form.twoCategoryId = null;
      this.form.threeCategoryId = null;
      this.formCategoryList2 = [];
      this.formCategoryList3 = [];
      if (!this.form.oneCategoryId) {
        return;
      }
      listOnInfo({
        type: 2,
        oneCategoryId: this.form.oneCategoryId,
      }).then((response) => {
        this.formCategoryList2 = response.data;
      });
    },
    formChangeTwo() {
      this.form.threeCategoryId = null;
      this.formCategoryList3 = [];
      if (!this.form.twoCategoryId) {
        return;
      }
      listOnInfo({
        type: 3,
        oneCategoryId: this.form.oneCategoryId,
        twoCategoryId: this.form.twoCategoryId,
      }).then((response) => {
        this.formCategoryList3 = response.data;
      });
    },
    formChangeThree() {
      this.form.categoryInfoId = this.formCategoryList3.find(
        (item) => item.id == this.form.threeCategoryId
      )?.categoryInfoId;
      //    this.form.categoryInfoId = this.form.threeCategoryId;
    },
    changeOne() {
      this.queryParams.twoCategoryId = null;
      this.queryParams.threeCategoryId = null;
      this.categoryList2 = [];
      this.categoryList3 = [];
      if (!this.queryParams.oneCategoryId) {
        return;
      }
      listOnInfo({
        type: 2,
        oneCategoryId: this.queryParams.oneCategoryId,
      }).then((response) => {
        this.categoryList2 = response.data;
      });
    },
    changeTwo() {
      this.queryParams.threeCategoryId = null;
      this.categoryList3 = [];
      if (!this.queryParams.twoCategoryId) {
        return;
      }
      listOnInfo({
        type: 3,
        oneCategoryId: this.queryParams.oneCategoryId,
        twoCategoryId: this.queryParams.twoCategoryId,
      }).then((response) => {
        this.categoryList3 = response.data;
      });
    },
    // 查询 一级 二级 三级 目录数据
    getListOnInfo() {
      listOnInfo({ type: 1 }).then((res) => {
        this.categoryList1 = res.data;
        this.formCategoryList1 = res.data;
      });
    },
    /** 质量中心--标准配比列表 */
    getList() {
      this.loading = true;
      listRatio(this.queryParams).then((response) => {
        this.ratioList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.editDisabled = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.activeName = "1";
      this.form = {
        id: null,
        type: null,
        oneCategoryId: null,
        twoCategoryId: null,
        threeCategoryId: null,
        categoryInfoId: null,
        name: null,
        plasticHoldingTime: null,
        intensityRating: null,
        benchmarkPrice: null,
        content: null,
        status: 0,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };

      this.form1 = {
        pldh: null,
        pmdh: null,
        bgmd: null,
        dldykzddj: null,
        kzddjykzzj: null,
        kdf: null,
        mdf: null,
        ksdj: null,
        klsydj: null,
        kllzstdj: null,
        kllzstdjdtlf: null,
        kthxndj: null,
        zmsxndjyxn: null,
        tsxs: null,
        kzdj: null,
        gmddj: null,
        xsldj: null,
        fhdj: null,
      };
      this.form2 = {
        pldh: null,
        pmdh: null,
        bgmd: null,
        cd: null,
        bsl: null,
        kdxqdssl: null,
        kdxzlssl: null,
        njlsqd: null,
        ssl: null,
        ksdj: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      console.log('dddd');
      
      this.$router.push({
        path: "/qualityCenter/ratioManage/edit",
      });
    },    
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: `/qualityCenter/ratioManage/edit?id=${row.id}`,
      });
    },
    handleClose() {
      this.editDisabled = false;
      this.cancel();
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.editDisabled = true;
      this.handleUpdate(row, "查看主标号");
    },
    /** 提交按钮 */
    async submitForm() {
      try {
        await this.$refs["form-left"].validate();
      } catch (error) {
        this.activeName = "1";
        return;
      }
      await Promise.all([
        this.$refs["form-type"].validate(),
        this.$refs["form-right"].validate(),
      ]);

      if (this.form.type == 1) {
        this.form.content = JSON.stringify(this.form1);
      }
      if (this.form.type == 2) {
        this.form.content = JSON.stringify(this.form2);
      }

      if (this.form.id != null) {
        updateDesignator(this.form).then((response) => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.resetQuery();
        });
      } else {
        addDesignator(this.form).then((response) => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.resetQuery();
        });
      }

      //   this.$refs["form"].validate(valid => {
      //     if (valid) {
      //       if (this.form.id != null) {
      //         updateDesignator(this.form).then(response => {
      //           this.$modal.msgSuccess("修改成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       } else {
      //         addDesignator(this.form).then(response => {
      //           this.$modal.msgSuccess("新增成功");
      //           this.open = false;
      //           this.getList();
      //         });
      //       }
      //     }
      //   });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除产品--主标号编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDesignator(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(item) {
      if (item === '按勾选导出') {
        if (this.ids.length === 0) return this.$modal.msgError("请选择至少一条数据");
        this.download(
          "service/ratio/section/export",
          {
            ...this.queryParams,
            'ids':this.ids.join(',')
          },
          `ratio_${new Date().getTime()}.xlsx`
        );
        return;
      }
      this.download(
        "service/ratio/export",
        {
          ...this.queryParams,
        },
        `ratio_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.edit-form {
  // display: flex;
  .form-label {
    font-size: 14px;
    font-weight: bold;
    line-height: 36px;
    padding-right: 30px;
    .form-red {
      color: #d23c26;
    }
  }
  .form-value {
    display: flex;
    //gap: 30px;
  }
}

::v-deep .el-tabs__content {
  max-height: 400px;
  overflow: auto;
}

.row-item {
  display: flex;
  // gap: 20px;
}
::v-deep .el-form-item__label {
  // width: 240px;
  text-align: right;
}
.form-right ::v-deep .el-input {
  width: 200px;
}
.button-margin {
  margin-right: 10px; /* 设置右边距为10px */
}
</style>